## Partitioning the hard drive

Create the following partitions

 1. [required] EFI system partition, must be the 1st partition on the drive.
 2. [required] main root partition
 3. [recommended] 2nd root partition
 4. [recommended] home partition

One of the root partitions is current, the other holds the previous version
of the system (for fallback purposes).

## Installation to existing partitions

 1. Check if the target root partition exists
 2. Check if the target EFI partition exists
 3. Verify the boot image.
    a. There must be 2 partitions: ESP and root.
    b. root must be ext4 and ESP must be fat.
 4. Clone the image boot root partition into the target partition:
    e2image -rap -c $img_root $dest_root
 5. Generate and remember a new UUID for the target root filesystem:
    new_uuid=`uuidgen`
 6. Adjust the UUID of the target root filesystem:
    tune2fs -U $new_uuid $dest_root
 7. Verify the target root filesystem:
    e2fsck -f -p $dest_root
 8. Resize the target root filesystem so it occupies the whole partition:
    resize2fs -p $dest_root
 9. Equip the target root filesystem with a journal:
    tune2fs -O has_journal $dest_root
10. Mount the target root filesystem:
    mount -t ext4 -o rw,data=ordered,errors=remount-ro,noatime $dest_root /mnt
11. Mount the target EFI filesystem. Take care to NOT make it case-sensitive:
    mount -t vfat -o rw,codepage=866,iocharset=ascii,utf8,noatime $dest_esp /mnt/boot/efi
12. Copy the state files to the target root filesystem. The list of state files
    includes, but is not limitied to,
    - /etc/machine-id
    - /etc/hostname 
    - /etc/fstab
    - /etc/passwd
    - /etc/shadow
    - /etc/group
    - /etc/gshadow
    - /root/.ssh/authorized_keys
    - /etc/ssh/ssh_host_*_key*
13. Adjust /mnt/etc/fstab -- insert the new root filesystem UUID.
14. Adjust /mnt/boot/grub/grub.cfg:
    a. Change the default entry to non-volatile
    b. Adjust `set rootfs_uuid=` directive according to the new_uuid
    c. If using main+backup root partitions: add an entry to boot
        the previous version of the system.
15. Re-install the grub boot loader. This is necessary since the UUID
    of the destination root filesystem does NOT match the one of
    the boot image rootfs.(Having two filesystems with the same UUID is BAD.
    Think of booting a rescue system from the installation media).
    a. If UEFI support BootNNNN variables properly: 
       grub-install --boot-directory=/mnt/boot --efi-directory=/mnt/boot/efi --bootloader-id=boringlinux
    b. Otherwise fall back to a "removable" layout:
       grub-install --boot-directory=/mnt/boot --efi-directory=/mnt/boot/efi --no-nvram --removable
16. Enable network related services:
    a. systemctl enable --root=/mnt systemd-networkd.service
    b. systemctl enable --root=/mnt systemd-resolved.service
17. Unmount target filesystems:
    a. umount /mnt/boot/efi
    b. umount /mnt
18. Reboot

## Installing to an empty drive

BIG RED WARNING: ANY DATA ON THE DRIVE WILL BE ERASED

 1. Wipe out any previous partition table(s):
    wipefs -a $dest_drive
 2. Partiotion the drive: GPT, 5 partitions:
    - EFI system partition, 128 MB
    - root partition, 20 GB
    - alternative root partition, 20 GB
    - swap, 4 GB
    - home, the rest of the drive
 3. Set dest_root to the 2nd partion:
    - nvme drive: dest_root=${dest_drive}p2
    - sata drive: dest_root=${dest_drive}2
 4. Clone the image boot root partition into the target partition:
    e2image -rap -c $img_root $dest_root
 5. Generate and remember a new UUID for the target root filesystem:
    new_uuid=`uuidgen`
 6. Adjust the UUID of the target root filesystem:
    tune2fs -U $new_uuid $dest_root 
 7. Verify the target root filesystem:
    e2fsck -f -p $dest_root
 8. Resize the target root filesystem so it occupies the whole partition:
    resize2fs -p $dest_root
 9. Equip the target root filesystem with a journal:
    tune2fs -O has_journal $dest_root
10. Mount the target root filesystem:
    mount -t ext4 -o rw,data=ordered,errors=remount-ro,noatime $dest_root /mnt
11. Mount the target EFI filesystem. Take care to NOT make it case-sensitive:
    mount -t vfat -o rw,codepage=866,iocharset=ascii,utf8,noatime $dest_esp /mnt/boot/efi
12. Generate new machine ID:
    dbus-uuidgen --ensure=/mnt/etc/machine-id
13. Configure hostname:
    echo new_hostname > /mnt/etc/hostname
14. Create /mnt/etc/fstab:
    cat > /mnt/etc/fstab <<-EOF
    UUID=$new_uuid / ext4 rw,data=ordered,noatime,errors=remount-ro 1 1
    EOF
15. Adjust /mnt/boot/grub/grub.cfg:
    a. Change the default entry to non-volatile
    b. Adjust `set rootfs_uuid=` directive according to the new_uuid
16. Install the grub boot loader: 
    a. If UEFI support BootNNNN variables properly: 
       grub-install --boot-directory=/mnt/boot --efi-directory=/mnt/boot/efi --bootloader-id=boringlinux
    b. Otherwise fall back to a "removable" layout:
       grub-install --boot-directory=/mnt/boot --efi-directory=/mnt/boot/efi --no-nvram --removable
17. Enable network related services:
    a. systemctl enable --root=/mnt systemd-networkd.service
    b. systemctl enable --root=/mnt systemd-resolved.service
18. (optional) put your public ssh key into /mnt/root/.ssh/authorized_keys
19. Unmount target filesystems:
    a. umount /mnt/boot/efi
    b. umount /mnt
20. Reboot
